package com.jt9003.controller;

import com.jt9003.pojo.Item_type;
import com.jt9003.pojo.Usage;
import com.jt9003.service.ItemTypeService;
import com.jt9003.utils.PageUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/item_type")
public class ItemTypeController {
    @Resource
    private ItemTypeService itemTypeService;


    //添加收费类型√
    @RequestMapping("/insertItemType")
    public Map<String, Object> insertItemType(String itemTypeName) {
        Map<String, Object> map = new HashMap();
        System.out.println("itemTypeName="+itemTypeName);
        int i = itemTypeService.insertItemType(itemTypeName);
        if (i > 0) {
            map.put("msg", true);
            map.put("data", itemTypeName);
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/deleteItemTypeById")
    public Map<String, Object> deleteItemTypeById(int itemTypeId) {
        Map<String, Object> map = new HashMap();
        int i = itemTypeService.deleteItemTypeById(itemTypeId);
        if (i > 0) {
            map.put("msg", true);
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/updateItemTypeById")
    public Map<String, Object> updateUsageById(Item_type item_type) {
        Map<String, Object> map = new HashMap();
        int i = itemTypeService.updateItemTypeById(item_type);
        System.out.println(item_type.getItemTypeId() + "---" + item_type.getItemTypeName());
        if (i > 0) {
            map.put("msg", true);
            map.put("data", item_type);
        } else {
            map.put("msg", false);
        }
        System.out.println("map="+map);
        return map;
    }

    @RequestMapping("/updateItemTypeStateById")
    public Map<String, Object> updateItemTypeStateById(Item_type item_type) {
        Map<String, Object> map = new HashMap();
        int i = itemTypeService.updateItemTypeStateById(item_type);
        System.out.println(i);
        if (i > 0) {
            map.put("msg", true);
            map.put("code", "0");
            map.put("data", item_type);
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/selectItem_typeByNameOrPinyinInitials")
    public Map<String, Object> selectItem_typeByNameOrPinyinInitials(String itemTypeName, @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                                                 @RequestParam(value = "limit", required = false, defaultValue = "5") Integer limit) {
        Map<String, Object> map = new HashMap();
        int start = PageUtils.getStart(page, limit);
        int count = itemTypeService.getItem_typeCountByResult(itemTypeName);
        List<Item_type> list = itemTypeService.selectItem_typeByNameOrPinyinInitials(itemTypeName, start, limit);
        if (list != null) {
            map.put("msg", "success");
            map.put("data", list);
            map.put("code", "0");
            map.put("count", count);
            map.put("", "");
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/getItem_typeCountByResult")
    public Map<String, Object> getItem_typeCountByResult(String itemTypeName) {
        Map<String, Object> map = new HashMap();
        int i = itemTypeService.getItem_typeCountByResult(itemTypeName);
        if (i > 0) {
            map.put("status", "200");
            map.put("message", "success");
            map.put("data", i);
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/selectItem_typeList")
    public Map<String, Object> selectItem_typeList(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                               @RequestParam(value = "limit", required = false, defaultValue = "5") Integer limit) {
        Map<String, Object> map = new HashMap();
        int start = PageUtils.getStart(page, limit);
        int count = itemTypeService.getItem_typeCount();
        List<Item_type> list = itemTypeService.selectItem_typeList(start, limit);
        if (list.size() > 0) {
            System.out.println("================start:" + start);
            map.put("msg", "success");
            map.put("data", list);
            map.put("code", "0");
            map.put("count", count);
            map.put("", "");
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/deleteItem_typeById")
    public Map<String, Object> deleteItem_typeById(String checkList) {
        Map<String, Object> map = new HashMap();
        System.out.println("==>" + checkList);

        String[] strs = checkList.split(",");
        System.out.println(strs[0]);
        List<Integer> ids = new ArrayList<>();
        for (String str : strs) {
            ids.add(Integer.parseInt(str));

        }

        int i = itemTypeService.deleteItem_typeById(ids);
        if (i > 0) {
            map.put("msg", true);
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/getItem_typeCount")
    public Map<String, Object> getItem_typeCount() {
        Map<String, Object> map = new HashMap();
        int i = itemTypeService.getItem_typeCount();
        if (i > 0) {
            map.put("status", "200");
            map.put("message", "success");
            map.put("data", i);
        } else {
            map.put("msg", "error");
        }
        return map;
    }


}
